package com.wanda.controller;


import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wanda.model.Apartment_LayoutModel;
import com.wanda.model.BuildModel;
import com.wanda.model.BuildProModel;
import com.wanda.model.JsonModelGeneral;
import com.wanda.model.RoomBaseModel;
import com.wanda.model.RoomModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.Apartment_LayoutService;
import com.wanda.service.BuildService;
import com.wanda.service.RoomService;
import com.wanda.util.CreateRoomUtilModel;
import com.wanda.util.DateFormat;
import com.wanda.util.RedisUtil;

import redis.clients.jedis.Jedis;

@Controller
@RequestMapping("qt/build")
public class CreateRoomController {

	
	@Autowired
	private BuildService buildService;
	
	@Autowired
	private Apartment_LayoutService apartment_LayoutService;
	
	@Autowired
	private RoomService roomService;


	// 添加楼栋信息
	@RequestMapping(value = "/addBuild.do") 
	public @ResponseBody Map<String, Object> add(String build_name, int floor_count, int room_count, String remark,
			String project_id, String delete_state) {

		System.out.println("进入添加");

		BuildModel model = new BuildModel();

		model.setBuild_name(build_name);
		model.setFloor_count(floor_count);
		model.setRoom_count(room_count);
		model.setRemark(remark);
		model.setProject_id(project_id);
		model.setDelete_state("0");

		buildService.insertBuild(model);

		Map<String, Object> map = new HashMap<String, Object>();

		System.out.println("添加成功");

		map.put("msg", "成功");

		return map;
	}

	// 查询楼栋所有信息
	@RequestMapping(value = "/selectAll.do")
	public ModelAndView showAll() {
		
		System.out.println("进入楼栋查询");
		ModelAndView model1 = new ModelAndView();
		List<BuildProModel> buildlist = null;
		try {
			buildlist = buildService.selectAll();
			model1.setViewName("/ht/listBuild.jsp");
			System.err.println("aaaaaa:"+buildlist.get(0).getProject().getProject_name());
			model1.addObject("buildlist", buildlist);
			System.out.println("记录长度："+model1);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return model1;
	}

	// 遍历单元表（房间）里需要的字段并返回(楼栋名字)
	@RequestMapping(value = "/selectAlls.do")
	public @ResponseBody Map<String, Object> showAlls() {

		Map<String, Object> map = new HashMap<String, Object>();

		List<BuildProModel> model = null;
		model = buildService.selectAll();
		map.put("msg", model);

		return map;
	}

	// 修改楼栋信息
	@RequestMapping(value = "/updateBuild.do")
	public String update(BuildModel model) {

		System.out.println("进入修改" + model.getBuild_name());
		model.setUpdate_time(DateFormat.getNowDate());
		buildService.updateBuild(model);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("buildList", map);
		return "redirect:/qt/ht/build/selectAll.do";

	}

	// 软删除一条记录
	@RequestMapping(value = "/deleteBuild.do")
	public String delete(BuildModel model,HttpServletRequest request) {

		System.out.println("进入删除");

		model.setDelete_time(DateFormat.getNowDate());
		model.setBuild_id(model.getBuild_id());
		Wd_UserModel be = (Wd_UserModel) request.getSession().getAttribute("bean");
	    model.setDelete_user(be.getName());
		
		int build = buildService.deleteBuild(model);
       

		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", build);
		return "redirect:/qt/ht/build/selectAll.do";
	}

	// 查询单条记录
	@RequestMapping(value = "/selectById.do")
	public ModelAndView selectById(String id) {

		System.out.println("进入单条查询");
		ModelAndView model = new ModelAndView();
		BuildModel build = null;
		try {
			build = buildService.selectById(id);
			model.setViewName("/ht/updateBuild.jsp");
			model.addObject("build", build);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return model;
	}

	// 拼接SQL查询
	@RequestMapping(value = "/selectBySql.do") 
	public @ResponseBody JsonModelGeneral<BuildModel> selectBySql(String SeacherSql) {

		System.err.println("SeacherSql:" + SeacherSql);

		JsonModelGeneral<BuildModel> json = new JsonModelGeneral<BuildModel>();
		List<BuildModel> builds = null;
		try {

			builds = buildService.selectBySql(SeacherSql);

			json.setRows(builds);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;

	}
	// 拼接SQL查询
	@RequestMapping(value = "/selectApartmentLayoutBySql.do") 
	public @ResponseBody JsonModelGeneral<Apartment_LayoutModel> selectApartmentLayoutBySql(String SeacherSql) {

		System.err.println("SeacherSql:" + SeacherSql);
		JsonModelGeneral<Apartment_LayoutModel> json = new JsonModelGeneral<Apartment_LayoutModel>();
		List<Apartment_LayoutModel> builds = null;
		try {

			builds = apartment_LayoutService.selectAll();

			json.setRows(builds);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;

	}
	// 添加楼栋信息
	@RequestMapping(value = "/addRoomredis.do")
	public @ResponseBody Map<String, Object> addRoomredis(@RequestBody CreateRoomUtilModel[] arr) {
		//Jedis jedis = RedisUtil.getJedis();
		Map<String, Object> map1 = new HashMap<String, Object>();
		Jedis jedis = RedisUtil.getJedis();
       long time1 = System.currentTimeMillis();		
		for(CreateRoomUtilModel cru:arr){
			Map<String, String> map= new HashMap<String, String>();
		    UUID uuid = UUID.randomUUID();
	    	map.put("huxing", cru.getHuxing());
	    	map.put("number",cru.getNumber());
	    	map.put("lid", cru.getLid());
	        jedis.hmset(uuid+"",map);
		}
		 long time2 = System.currentTimeMillis();	
	
		 System.out.println(time1+"-"+time2+"="+(time1-time2));
		 
        RedisUtil.returnResource(jedis);
	
		System.out.println("添加成功");
		map1.put("msg", "成功");
		return map1;
	}
		@RequestMapping(value = "/mysql/addRoomredis.do")
		public @ResponseBody Map<String, Object> addmysqlRoomredis(@RequestBody CreateRoomUtilModel[] arr) {
			RoomModel rm = new RoomModel();
			  long time1 = System.currentTimeMillis();		
			for(CreateRoomUtilModel cru:arr){
				rm.setRoom_code(cru.getNumber());
				rm.setBuild_id(cru.getLid());
                rm.setUnit_id(cru.getUnit());
                rm.setFloor(cru.getFloor());
				roomService.insertRoom(rm);
			}
			 long time2 = System.currentTimeMillis();	
			 System.out.println(time1+"-"+time2+"="+(time1-time2));
			Map<String, Object> map1 = new HashMap<String, Object>();
			System.out.println("添加成功");
			map1.put("msg", "成功");
			return map1;
		}
}
